var vm = new Vue({
	el:'#rrapp',
	data:{
		brandId: null,
		brandName: null,
		articleDesc: null,
		articleNewDesc: null,
		index: null
	},
	methods: {
		// 更新文章概述
		updateArticleDesc: function(t) {
			// 1、参数校验
			if (!this.validateParam()) {
				return;
			}

			let title = "";
			if (t == 1) {
				title = "概述";
			} else if (t == 2) {
				title = "标题";
			}
			let msg = "原" + title + "【" + vm.articleDesc + "】<br\>替换为<br\>新" + title + "【" + vm.articleNewDesc + "】";

			layer.confirm("您确定要替换文章的" + title + "吗？<br\><br\>" + msg, {icon: 0, title:'提示', area: ['500px', '300px']}, function (index) {
				layer.close(index);
				// 2、ajax 请求：更新文章标题
				$.ajax({
					type: "POST",
					url: baseURL + 'sys/barticleinfo/updateArticleDesc',
					traditional: true,
					data: {"articleDesc": vm.articleDesc, "articleNewDesc": vm.articleNewDesc.split(","), "type": t},
					success: function(r) {

					}
				});

				// 3、打开进度条窗口
				vm.progressView();

				// 4、按固定时间请求一次服务器，获取当前进度条数据
				let total = 0;
				let timer = setInterval(function () {
					$.ajax({
						type: "GET",
						url: baseURL + 'sys/barticleinfo/getProgressInfo',
						contentType: "application/json;charset=UTF-8",
						success: function(r) {
							debugger
							total = r.progressInfo.total;// 总任务数量
							let current = r.progressInfo.current;// 当前数量
							let percent = r.progressInfo.percent;// 当前百分比
							let flag = r.progressInfo.flag;// true: 任务数为0

							$("#progressDiv span").eq(0).text(" " + current + " / " + total);
							$("#progressDiv .progress-bar").eq(0).text(percent + "%");
							$("#progressDiv .progress-bar").eq(0).css("width", percent + "%");
							$("#progressDiv .progress-bar").eq(0).attr("aria-valuenow", percent);
							if (current != 0 && current == total) {// 任务完成
								// 关闭定时函数
								clearInterval(timer);
								// 提示窗口
								layer.open({
									type: 0,
									title: '提示',
									content: '替换已完成',
									closeBtn: 0,
									yes: function(index, layero){
										layer.close(vm.index);// 关闭进度条窗口
										layer.close(index);// 关闭提示窗口
									}
								});
							}
							if (flag) {
								// 关闭定时函数
								clearInterval(timer);
								// 提示窗口
								layer.open({
									type: 0,
									title: '提示',
									content: '没有匹配的数据',
									closeBtn: 0,
									yes: function(index, layero){
										layer.close(vm.index);// 关闭进度条窗口
										layer.close(index);// 关闭提示窗口
									}
								});
							}
						}
					});
				}, 1000);
			});
		},
		// 参数检验
		validateParam: function() {
			// 1、失去焦点
			// $("#selectBrandName").blur();
			$("#articleDesc").blur();
			$("#articleNewDesc").blur();

			// 2、非空判断
			let flag = false;
			/*if (this.brandId == null || this.brandId == '') {
				alert("品牌不能为空！");
				return flag;
			}*/
			if (this.articleDesc == null || this.articleDesc == '') {
				alert("文章原标题不能为空！");
				return flag;
			}
			if (this.articleNewDesc == null || this.articleNewDesc == '') {
				alert("文章新标题不能为空！");
				return flag;
			}
			if (this.articleDesc == this.articleNewDesc) {
				alert("原标题和新标题不能一样");
				return flag;
			}
			return !flag;
		},
		progressView: function () {
			// 初始化进度条的值
			$("#progressDiv span").eq(0).text("初始化...");
			$("#progressDiv .progress-bar").eq(0).text("0%");
			$("#progressDiv .progress-bar").eq(0).css("width", "0%");
			$("#progressDiv .progress-bar").eq(0).attr("aria-valuenow", "0");
			$("#pdetail").html();

			vm.index = layer.open({
				type: 1,
				title: '更新文章',
				area: ["800px", "200px"],
				closeBtn: 0,
				shadeClose: false,
				content: $("#progressView"),
				anim: 2
			});
		}
	}
});